// pages/MonthlyPay/ComputingResult/ComputingResult.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Price: '',//物業價格
    loanType: '',
    loan: '',//貸款
    rate: '',//年利率
    timelimit: '',//還款期
    loanpercent: '',
    Firstphase: '',//首期
    Firstphasepercent:'',
    rate:'',//年利率
    installment:''//月供
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync('kMonthrate');
    wx.removeStorageSync('kinstallment');
    wx.removeStorageSync('Amount'); 
    var Amount;//貸款金額
    // var loanper//貸款金額百分比
    var _firstph //首付
    var kPrice = wx.getStorageSync('kPrice');//物業價格
    var kloan = wx.getStorageSync('kloan');//貸款
    var kloanType = wx.getStorageSync('kloanType');
   
    if (kloanType == "1")//金額
    {

      Amount = kloan;
    }
    if (kloanType == "2")//比例
    {
      Amount = (parseFloat(kloan)/100) * parseFloat(kPrice);
    }
   
    var krate = wx.getStorageSync('krate');//年利率
    var ktimelimit = wx.getStorageSync('ktimelimit');//還款期
    _firstph = ((parseFloat(kPrice) - parseFloat(Amount)))//首期：即房价-贷款金额
    var Mlimit = (parseInt(ktimelimit) * 12)//期数
    var Monthrate = (parseFloat(krate)/100)/12;//月利率
    var kinstallment = ((Amount * 10000) * Monthrate * (Math.pow((1 + Monthrate), Mlimit)) / (Math.pow((1 + Monthrate), Mlimit) - 1));//月供
    if (isNaN(kinstallment))
      kinstallment=0
    var _loanpercent = ((parseFloat(Amount) / parseFloat(kPrice)).toFixed(2)) * 100 //貸款金額/物業金額就是占百分比
    var _loan = Amount;
    if (Amount > 0)
      _loan = util.formatCurrency((Amount * 10000))
    this.setData({
      Price: '$' + util.formatCurrency((kPrice * 10000)),
      loan: '$' + _loan,
      timelimit: ktimelimit + '(' + Mlimit + '期)',
      loanpercent: '(' + _loanpercent + '%)', //貸款金額/物業金額就是占百分比
      Firstphase: '$' + util.formatCurrency(_firstph *10000),
      Firstphasepercent: '(' +(parseFloat(_firstph) / parseFloat(kPrice).toFixed(2)) * 100 + '%)',
      rate: krate+'%',
      installment: '$' + util.formatCurrency(kinstallment.toFixed(2)),  //月供 = [贷款额×月利率×（1+月利率）^期数]÷[（1+月利率）^期数－1]/// (Math.pow((1 + Monthrate), (Mlimit)) - 1)
    })
    wx.setStorageSync('kMonthrate', Monthrate);
    wx.setStorageSync('kinstallment',kinstallment);
    wx.setStorageSync('Amount', Amount); 
  },

  RepaymentTableTap:function(){
     wx.navigateTo({
       url: '/pages/MonthlyPay/RepaymentTable/RepaymentTable',
     })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})